<?php
/**
 * @desc 		初始化该类时,提供默认的路径名
 * 				@param  $inc_path 目录级别,主要有"./"和"../"
 * 				@param  $app_dir_name 主程序目录名称,默认为"app/"下面
 *  			@Expected 肯定有更好的方法来处理smarty的资源路进个问题,欢迎提出意见
 * @name 		app/include/class.my.smarty.php
 * @version 	Since 1.0beta
 * @author 		Stevenchow811@163.com
 * @link 		http://www.ualink.cn
 * @final 		2008-1-9 10:19
 * @copyright 	本软件允许您随意传播,但请保留作者的版权,
 * 			  	如果有任何版权疑问,请联系作者 stevenchow811@163.com
 */

//require (SMARTY_DIR . "Smarty.class.php");
require_once($inc_path .APP_NAME. "plugins/smarty/Smarty.class.php");

class MySmarty extends Smarty {
	
	/**
//php4 不支持public
	 */
	var $app_dirname;
	var $inc_path_sign;
	var $default_tpl_dirname = "";
	var $media_dir;
	var $tmp_dir;
	
	function MySmarty($inc_path = "./", $app_dir_name = "app") {
		global $ua_sm_compile_dir;
		$this->Smarty ();
		if (! empty ( $inc_path ))
			$this->setIncPath ( $inc_path );
		if (! empty ( $app_dir_name ))
			$this->setAppDir ( $app_dir_name );
		$NowPathArray = explode ( $app_dir_name, str_replace ( "\\", "/", dirname ( __FILE__ ) ) );
		$AppDir = $NowPathArray [0] . $app_dir_name;
		$TmpDir = $NowPathArray [0] . "media/tmp";
		$this->plugins_dir [] = $AppDir . "/vendors/";
		$this->template_dir = $inc_path . "templates/" . $this->default_tpl_dirname;
		$this->media_dir = $inc_path . "templates/" . $this->default_tpl_dirname;
		if (empty ( $ua_sm_compile_dir )) {
			$this->compile_dir = $TmpDir . "/templates_c/";
		} else {
			$comp_d = $TmpDir . "/templates_c/" . $ua_sm_compile_dir;
			if (! file_exists ( $comp_d ))
				@mkdir ( $comp_d );
			$this->compile_dir = $comp_d;
		}
		$this->config_dir = $AppDir . "/configs/";
		$this->cache_dir = $TmpDir . "/cache/";
		//$this->assign("media_path",$smarty->template_dir);
		// 如果你想修改Smarty的调用语法为{**}，下面就可以
		$this->left_delimiter = "<{";
		$this->right_delimiter = "}>";
		//缓存默认值是3600秒，你可以通过设置$caching=2来控制单个缓存文件各自的的过期时间，例如：
		//$smarty->caching = 2;$smarty->cache_lifetime = 300;//缓存时间是5分钟
		
    $this->caching = false;
		$this->debugging = false;
		
    //$this->force_compile = true;
	//$this->load_filter('pre','chpath');
	}
	
	function setAppDir($dirname) {
		$this->app_dirname = $dirname;
	}
	
	function getAppDir() {
		return $this->app_dirname;
	}
	
	function setIncPath($inc_path) {
		$this->inc_path_sign = $inc_path;
	}
	
	function getIncPath() {
		return $this->inc_path_sign;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $file_name
	 * @param unknown_type $content
	 * @param 是否自动循环产生静态文件 $htmlcache
	 * @return boolean，是，则操作htmlcache表
	 */
	function MakeHtmlFile($file_name, $content = "", $cache_check = false) {
		global $htmlcache;
		global $_GET, $ua_user;
		if (! uaStrCompare ( md5 ( AUTH_KEY ), $_GET ['token'] )) {
			//			goto(URL."message.php?message=".urlencode("Passport Key Error"));
		}
		unset ( $ua_user );
		if (! $fp = fopen ( $file_name, "w" )) {
			die ( "File Or Folder Open Error : " . $file_name );
			return false;
		}
		if (! fwrite ( $fp, $content )) {
			fclose ( $fp );
			die ( "File Write Error : " . $file_name );
			return false;
		}
		if ($cache_check) {
			$htmlcache->updateCacheTime ();
			if (STATIC_HTML_LEVEL > 0)
				goto ( $file_name );
			else {
				goto ( $_SERVER ['PHP_SELF'] );
			}
		}
		fclose ( $fp );
		chmod ( $file_name, 0666 );
		return true;
	}
	
	function getRelativePath() {
		//../../index.html
		global $_GET, $theme_name;
		$paths = array ();
		//$inc_path = $this->getIncPath();
		$currentRelativePath = $this->getIncPath ();
		if (isset ( $_GET ['action'] ) && ($_GET ['action']) == "html") {
			//$currentRelativePath = str_replace("../", "../../", $currentRelativePath);
			if ($currentRelativePath == "./")
				$currentRelativePath = "../";
			elseif ($currentRelativePath == "../")
				$currentRelativePath = "../../";
			$paths ['CSS_PATH'] = $paths ['JS_PATH'] = $paths ['INC_PATH'] = $currentRelativePath;
			$paths ['media_path'] = $currentRelativePath . "templates/" . $this->default_tpl_dirname;
			$paths ['theme_media_path'] = $currentRelativePath . "templates/" . $this->default_tpl_dirname . $theme_name . "/";
			$paths ['ProductPicturePath'] = $currentRelativePath . "media/products/";
			$paths ['TradePicturePath'] = $currentRelativePath . "media/trades/";
		} else {
			$paths ['CSS_PATH'] = $paths ['JS_PATH'] = $paths ['INC_PATH'] = $currentRelativePath;
			$paths ['media_path'] = $this->media_dir;
			$paths ['theme_media_path'] = $this->media_dir . $theme_name . "/";
			$paths ['ProductPicturePath'] = $currentRelativePath . "media/products/";
			$paths ['TradePicturePath'] = $currentRelativePath . "media/trades/";
		}
		return $paths;
	}
	
	function getAbsolutePath() {
		global $theme_name;
		$paths = array ();
		$currentRelativePath = substr ( URL, 0, - 1 );
		$paths ['CSS_PATH'] = $paths ['JS_PATH'] = $paths ['INC_PATH'] = $paths ['media_path'] = $currentRelativePath . "/";
		$paths ['theme_media_path'] = URL . "templates/" . $theme_name . "/";
		$paths ['ProductPicturePath'] = $currentRelativePath . "/media/products/";
		$paths ['TradePicturePath'] = $currentRelativePath . "/media/trades/";
		return $paths;
	}
}
?>
